package org.example;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;

/**
 * 测试Vintage波状图模式
 */
public class TestVintageWavePattern {
    
    public static void main(String[] args) {
        System.out.println("=== Vintage波状图模式测试 ===");
        
        // 测试期数逾期率计算
        System.out.println("期数逾期率计算：");
        for (int period = 1; period <= 24; period++) {
            double rate = calculatePeriodOverdueRate(period);
            System.out.printf("第%d期: %.2f%%\n", period, rate * 100);
        }
        
        // 计算数据量
        System.out.println("\n=== 数据量计算 ===");
        LocalDate startDate = LocalDate.of(2023, 7, 1);
        LocalDate currentDate = LocalDate.now();
        int baseOrders = 50000;
        int monthlyIncrease = 5000;
        int avgLoanAmount = 5000; // 平均5000元
        
        LocalDate month = startDate;
        int totalOrders = 0;
        int monthCount = 0;
        
        while (!month.isAfter(currentDate)) {
            int monthsFromStart = (int) (month.toEpochDay() - startDate.toEpochDay()) / 30;
            int monthlyOrders = baseOrders + (monthsFromStart * monthlyIncrease);
            totalOrders += monthlyOrders;
            monthCount++;
            
            System.out.println("月份: " + month + ", 订单数: " + monthlyOrders);
            
            month = month.plusMonths(1);
        }
        
        BigDecimal totalLoanAmount = BigDecimal.valueOf(totalOrders).multiply(BigDecimal.valueOf(avgLoanAmount));
        
        // 基于新的波状图模式估算在途余额
        // 假设平均逾期率为5%（基于波状图的平均值），平均剩余80%
        BigDecimal avgOverdueRate = new BigDecimal("0.05"); // 平均5%逾期率
        BigDecimal remainingRate = new BigDecimal("0.80"); // 平均剩余80%
        BigDecimal estimatedOutstanding = totalLoanAmount.multiply(avgOverdueRate).multiply(remainingRate);
        
        System.out.println("\n=== 计算结果 ===");
        System.out.println("总月数: " + monthCount);
        System.out.println("总订单数: " + totalOrders);
        System.out.println("平均贷款金额: " + avgLoanAmount + " 元");
        System.out.println("总贷款金额: " + totalLoanAmount.divide(new BigDecimal("100000000"), 2, RoundingMode.HALF_UP) + " 亿");
        System.out.println("估算在途余额: " + estimatedOutstanding.divide(new BigDecimal("100000000"), 2, RoundingMode.HALF_UP) + " 亿");
    }
    
    /**
     * 根据期数计算逾期率（基于真实业务数据的波状图模式）
     */
    private static double calculatePeriodOverdueRate(int period) {
        double FIRST_PERIOD_OVERDUE_RATE = 0.001; // 0.1%
        double PEAK_OVERDUE_RATE = 0.0736; // 7.36%
        
        if (period == 1) {
            return FIRST_PERIOD_OVERDUE_RATE; // 0.1%
        } else if (period <= 2) {
            return 0.046; // 4.6%
        } else if (period <= 10) {
            // 从第2期到第10期逐渐增加到峰值
            double progress = (period - 2.0) / 8.0; // 0到1的进度
            return 0.046 + (PEAK_OVERDUE_RATE - 0.046) * progress;
        } else {
            // 从第10期开始逐渐下降
            double progress = Math.min((period - 10.0) / 14.0, 1.0); // 0到1的进度，最多到第24期
            return PEAK_OVERDUE_RATE - (PEAK_OVERDUE_RATE - 0.0456) * progress;
        }
    }
}




